import grace from "../../../utils/grace.js";
import {Cart} from 'cart-model.js';

let cart = new Cart();

grace.page({

    /**
     * 页面的初始数据
     */
    data: {
        carts: [],
        amountTotal: 0,
        selectAllText: '全选',
        selectCount: 0,
        selectCartIds: []
    },
    onShow() {
        let token = wx.getStorageSync('token');
        // token校验
        if (!token) {
            wx.removeStorageSync('userInfo');
            wx.navigateTo({
                url: '/pages/common/authorize/index'
            })
        } else {
            // 查询购物车
            cart.getCartData({'token': wx.getStorageSync('token')}, (data) => {
                if (data != undefined) {
                    this.$data.carts = data;
                    this.refreshInfo();
                }
            })
        }
    },
    /* 点击减号 */
    bindMinus(e) {
        for (let i = 0; i < this.$data.carts.length; i++) {
            if (this.$data.carts[i].id == e.currentTarget.dataset.id) {
                if (this.$data.carts[i].product_uom_qty > 1) {
                    this.$data.carts[i].product_uom_qty -= 1;
                    this.updateCart(this.$data.carts[i].id, this.$data.carts[i].product_uom_qty);
                    this.refreshInfo();
                }
            }
        }
    },
    bindPlus(e) {
        for (let i = 0; i < this.$data.carts.length; i++) {
            if (this.$data.carts[i].id == e.currentTarget.dataset.id) {
                this.$data.carts[i].product_uom_qty += 1;
                this.updateCart(this.$data.carts[i].id, this.$data.carts[i].product_uom_qty);
                this.refreshInfo();
            }
        }
    },
    /* 输入框事件 */
    bindManual(e) {
        let num = e.detail.value;
        if (isNaN(num) || num < 1) {
            num = 1;
        }
        for (let i = 0; i < this.$data.carts.length; i++) {
            if (this.$data.carts[i].id == e.currentTarget.dataset.id) {
                this.$data.carts[i].product_uom_qty = num;
                this.updateCart(this.$data.carts[i].id, this.$data.carts[i].product_uom_qty);
                this.refreshInfo();
            }
        }
    },
    updateCart(cart_id, product_uom_qty) {
        let that = this;
        cart.editCartData({
            'token': wx.getStorageSync('token'),
            'cart_id': cart_id,
            'product_uom_qty': product_uom_qty
        }, (data) => {
            if (data.code == 0) {
                for (let i = 0; i < that.$data.carts.length; i++) {
                    if (that.$data.carts[i].id == cart_id) {
                        that.$data.carts[i].price_unit = data.data.price_unit;
                    }
                }
            }
        })
    },
    refreshInfo() {
        let amountTotal = 0;
        let selectCount = 0;
        let selectCartIds = [];
        for (let i = 0; i < this.$data.carts.length; i++) {
            if (this.$data.carts[i].checked) {
                amountTotal += this.$data.carts[i].price_unit * this.$data.carts[i].product_uom_qty;
                selectCount += 1;
                selectCartIds.push(this.$data.carts[i].id);
            }
            let minusStatus = this.$data.carts[i].product_uom_qty <= 1 ? 'disabled' : 'normal';
            this.$data.carts[i].minusStatus = minusStatus;
        }
        this.$data = {amountTotal: amountTotal, selectCount: selectCount, selectCartIds: selectCartIds};
    },
    removeItem(e) {
        cart.deleteCartData({'cart_id': e.currentTarget.dataset.id, 'token': wx.getStorageSync('token')}, (data) => {
            if (data.code == 0) {
                for (let i = 0; i < this.$data.carts.length; i++) {
                    if (this.$data.carts[i].id == e.currentTarget.dataset.id) {
                        this.$data.carts.splice(i, 1);
                    }
                }
            }
        })
    },
    checkboxChange(e) {
        for (let i = 0; i < this.$data.carts.length; i++) {
            let is_selected = false;
            for (let j = 0; j < e.detail.value.length; j++) {
                if (this.$data.carts[i].id == e.detail.value[j]) {
                    is_selected = true;
                    break;
                }
            }
            if (is_selected) {
                this.$data.carts[i]['checked'] = true;
            } else {
                this.$data.carts[i]['checked'] = false;
            }
        }
        this.refreshInfo();
    },
    checkboxChangeAll() {
        for (let i = 0; i < this.$data.carts.length; i++) {
            if (this.$data.selectAllText == '全选') {
                this.$data.carts[i]['checked'] = true;
            } else {
                this.$data.carts[i]['checked'] = false;
            }
        }
        if (this.$data.selectAllText == '全选') {
            this.$data.selectAllText = '反选';
        } else {
            this.$data.selectAllText = '全选';
        }
        this.refreshInfo();
    },
    confirm() {
        if (this.$data.selectCartIds.length > 0) {
            wx.navigateTo({
                url: '/pages/mall/confirm/index?cart_ids=' + this.$data.selectCartIds
            })
        }
    },
    gotoCategory() {
        wx.switchTab({
            url: '/pages/mall/category/index'
        })
    }
})